package com.example.wan_android.viewmodel

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.wan_android.http.bean.AuthorDatumData
import com.example.wan_android.http.bean.AuthorNameItemData
import com.example.wan_android.http.code.AuthorNameRepository
import com.example.wan_android.http.code.AuthorRepository
import com.example.wan_android.ui.activity.WebActivity
import kotlinx.coroutines.launch

class AuthorViewModel : BaseViewModel() {
    private val tabRepository = AuthorNameRepository()
    private val contentRepository = AuthorRepository()
    private val _tabLiveData = MutableLiveData<Result<List<AuthorNameItemData>>>()
    val tabLiveData = _tabLiveData
    private val _contentLiveData = MutableLiveData<Result<List<AuthorDatumData>>>()
    val contentLiveData = _contentLiveData
    val position = MutableLiveData<Int>(0)
    val currentIndex = MutableLiveData<Int>(0)
    val currentAuthor = MutableLiveData<String>("鸿洋")
    val isClear = MutableLiveData<Boolean>(false)
    private val _isSuccess = MutableLiveData<Boolean>(false)
    val isSuccess = _isSuccess


    fun setClear(clear: Boolean) {
        isClear.value = clear
    }

    fun setCurrentIndex(index: Int) {
        currentIndex.value = index
    }

    fun setCurrentAuthor(author: String) {
        currentAuthor.value = author
    }

    fun setPosition(p: Int) {
        position.value = p
    }

    init {
        getAuthorNameData()
        getAuthorArticleInfoData()
    }

    fun getAuthorNameData() {
        viewModelScope.launch {
            val result = tabRepository.fetchAuthorNameData()
            _tabLiveData.value = result
        }
    }

    fun getAuthorArticleInfoData() {
        viewModelScope.launch {
            contentRepository.fetchAuthorInfoData(
                position.value ?: 0, // 使用当前页码
                currentAuthor.value!!
            ).let { result ->
                if (result.isSuccess) {
                    result.getOrNull()?.let { data ->
                        if (data.isNotEmpty()) {
                            //  position.value = (position.value ?: 0) + 1 // 自动递增页码
                        }
                    }
                }
                _isSuccess.value = true
                _contentLiveData.value = result
            }
        }
    }

//    fun getAuthorArticleInfoData(author: String) {
//        viewModelScope.launch {
//            // 使用局部变量避免污染成员变量
//            var currentIndex = 0
//            // 创建临时可变集合
//            val accumulatedList = mutableListOf<AuthorDatumData>()
//
//            try {
//                do {
//                    // 带线程调度的网络请求
//                    val result = withContext(Dispatchers.IO) {
//                        contentRepository.fetchAuthorInfoData(
//                            currentIndex,
//                            author
//                        )
//                    }
//
//                    // 成功时处理数据
//                    if (result.isSuccess) {
//                        result.getOrNull()?.let { data ->
//                            if (data.isNotEmpty()) {
//                                accumulatedList.addAll(data)
//                                currentIndex++  // 索引自增准备下次请求
//                            } else {
//                                return@let  // 数据为空时退出循环
//                            }
//                        }
//                    } else {
//                        // 失败时传递错误
//                        _contentLiveData.postValue(result)
//                        return@launch
//                    }
//                } while (result.getOrNull()?.isNotEmpty() == true)
//
//                // 最终传递合并结果
//                _contentLiveData.postValue(Result.success(accumulatedList))
//
//            } catch (e: Exception) {
//                // 异常处理
//                _contentLiveData.postValue(Result.failure(e))
//            }
//        }
//    }

    fun openWeb(url: String) {
        startActivity(Intent().apply {
            putExtra(WebViewModel.url, url)
        }, targetClass = WebActivity::class.java)
    }
}